package com.qububu.db.dao.impl;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.base.dao.impl.BaseDaoImpl;
import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.common.user.dto.UserInfoDto.Sex;
import com.qububu.common.user.dto.UserLoginInfoDto;
import com.qububu.db.dao.UserInfoDao;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

/**
 * @author ZCW
 * 
 * @功能 与用户数据有关的数据库操作
 * */
@Repository
public class UserInfoDaoImpl extends BaseDaoImpl implements UserInfoDao {

	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	/**
	 * @功能 根据用户的id和密码查询用户信息
	 * @param loginInfo 用户登录信息
	 * @return UserInfoDto 用户信息 
	 * */
	public UserInfoDto getUserByIdAndPwd(UserLoginInfoDto loginInfo) {
		String sql = "select user_id from user_login where login_id=? and password=? and login_type=? limit 1";
		
		Object[] val = { loginInfo.getLoginId(), loginInfo.getPsw(), loginInfo.getLoginType().toIntValue() };
		List<Long> result = jdbcTemplate.queryForList(sql, val, Long.class);
		
		if (result.size() > 0) {
			Long userId = result.get(0);
			sql = "select * from user_info where user_id=? limit 1";
			Map<String, Object> elem = jdbcTemplate.queryForMap(sql, new Object[]{userId});
			
			UserInfoDto user  = new UserInfoDto((Long) elem.get("user_id"), (Long) elem.get("position_id"));
			
			user.setUserBirthday((Integer)elem.get("user_birthday") );
			user.setUserSex(Sex.getSex((Integer)elem.get("user_sex") ) );
			user.setNickName((String)elem.get("user_nickname") );
			user.setUserLogoUrl((String) elem.get("user_logo_url"));
//			user.setUserRegistDttm((Integer)elem.get("user_regist_dttm") );
			user.setUserEamil(loginInfo.getLoginId() );
			
			return user;
		} else{
			return null;
		}
	}

	/**
	 * @功能 根据登录id查询该用户是否已经存在
	 * @param loginId 用户的登录ID
	 * @return boolean false: 已经存在不可再用, true: 该用户不存在该邮箱可用
	 * */
	public boolean doCheckUserIdAlreadyExist(String loginId) {
		
		String sql = "select login_id from user_login where login_id = ? limit 1";
		Object[] val = {loginId};
		
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sql,val);
		
		if(result.size() == 1){
			return false;
		}
		else{
			return true;
		}
	}

	/**
	 * @功能 添加用户
	 * @param loginInfo 用户登录信息
	 * @param userInfo 用户信息
	 * @return String 新添加用户的id
	 * */
	public long doAddUser(UserLoginInfoDto loginInfo, UserInfoDto userInfo) {
		String sql1 = "insert into user_login(login_id, password, login_type) values(?, ?, ?)";
		Object[] val1 = {loginInfo.getLoginId(), loginInfo.getPsw(), loginInfo.getLoginType().toIntValue() };
		jdbcTemplate.update(sql1, val1);
		
		//get the id of insert.
		Long userId = jdbcTemplate.queryForLong("select LAST_INSERT_ID()");
		
		if (userId == 0) {
			userId = jdbcTemplate.queryForLong("SELECT user_id FROM user_login WHERE login_id = ?",
														new Object[]{loginInfo.getLoginId()}
													);
		}
		
		String sqlToUserInfo = "insert into user_info(user_id, user_sex, user_birthday, user_email, user_regist_dttm, user_regist_ip, user_nickname, user_logo_url, position_id) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		long positionId = ParamConverter.convertToLongObj(
				SystemUtil.decrypt(userInfo.getPositionId() ) );
		Object[] userInfoValues = {userId,
						userInfo.getUserSex().toIntValue(),
						userInfo.getUserBirthday(),
						loginInfo.getLoginId(),
						userInfo.getUserRegistDttm(),
						userInfo.getUserRegistIp(),
						userInfo.getNickName(),
						userInfo.getUserLogoUrl(),
						positionId
						};
		
		jdbcTemplate.update(sqlToUserInfo, userInfoValues);
		
		return userId;
	}

	/**
	 * @author HQ
	 * @功能 修改用户
	 */
	public void doModifyUser(UserInfoDto userInfoDto) {
		String sql = "update user_info set user_sex = ?, user_birthday = ?, user_email = ?, user_nickname = ?, user_logo_url = ?, position_id = ? where user_id = ?";
		
		long userId = ParamConverter.convertToLongObj(SystemUtil.decrypt(userInfoDto.getUserId() ) );
		long positionId = ParamConverter.convertToLongObj(SystemUtil.decrypt(userInfoDto.getPositionId() ) );
		
		Object[] userInfo = {
				userInfoDto.getUserSex().toIntValue(),
				userInfoDto.getUserBirthday(),
				userInfoDto.getUserEamil(),
				userInfoDto.getNickName(),
				userInfoDto.getUserLogoUrl(),
				positionId,
				userId
						};
		
		jdbcTemplate.update(sql, userInfo);

	}

}
